package com.tanhua.server.controller;

import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.MovementVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 圈子模块-控制层
 */
@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     * 动态-发布
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity postMovement(MovementVo movementVo, MultipartFile[] imageContent){
        movementService.postMovement(movementVo,imageContent);
        return ResponseEntity.ok(null);
    }


    /**
     * 用户动态
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResponseEntity findPageByUserId(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pagesize", defaultValue = "10") Long pagesize,Long userId){
        PageResult<MovementsVo> movementsVoPageResult =  movementService.findPageByUserId(page,pagesize,userId);
        return ResponseEntity.ok(movementsVoPageResult);
    }

    /**
     * 好友动态
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity movements(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pagesize", defaultValue = "10") Long pagesize){
        PageResult<MovementsVo> movementsVoPageResult =  movementService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(movementsVoPageResult);
    }

    /**
     * 推荐动态
     */
    @RequestMapping(value = "/recommend",method = RequestMethod.GET)
    public ResponseEntity recommend(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pagesize", defaultValue = "10") Long pagesize){
        PageResult<MovementsVo> movementsVoPageResult =  movementService.recommend(page,pagesize);
        return ResponseEntity.ok(movementsVoPageResult);
    }

    /**
     * 单条动态查询
     */
    @RequestMapping(value = "/{movementId}",method = RequestMethod.GET)
    public ResponseEntity findById(@PathVariable("movementId") String  movementId){
        MovementsVo movementsVo = movementService.findById(movementId);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 动态点赞
     */
    @RequestMapping(value = "/{movementId}/like",method = RequestMethod.GET)
    public ResponseEntity like(@PathVariable("movementId") String  movementId){
        Integer count = movementService.like(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态取消点赞
     */
    @RequestMapping(value = "/{movementId}/dislike",method = RequestMethod.GET)
    public ResponseEntity dislike(@PathVariable("movementId") String  movementId){
        Integer count = movementService.dislike(movementId);
        return ResponseEntity.ok(count);
    }


    /**
     * 动态喜欢
     */
    @RequestMapping(value = "/{movementId}/love",method = RequestMethod.GET)
    public ResponseEntity love(@PathVariable("movementId") String  movementId){
        Integer count = movementService.love(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态取消喜欢
     */
    @RequestMapping(value = "/{movementId}/unlove",method = RequestMethod.GET)
    public ResponseEntity unlove(@PathVariable("movementId") String  movementId){
        Integer count = movementService.unlove(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 谁看过我
     */
    @RequestMapping(value = "/visitors",method = RequestMethod.GET)
    public ResponseEntity visitors(){
        List<VisitorsVo> visitorsList = movementService.findLast4Visitors();
        return ResponseEntity.ok(visitorsList);
    }
}
